<template>
	<HandleTemplate
		:chartData="chartData"
		:abnormalData="abnormalData"
		:currentKey="abnormalId"
		@handleClick="handleClick"
		:menus="menus"
		:loading="loading"
		:listQuery="listQuery"
		@submit="submit"
	>
	</HandleTemplate>
</template>

<script>
import HandleTemplate from "@/template/handle.temp";
import {
	getAbnormal,
	insertAlarmHandle
} from "@/api/RCIM/RCIM-nuclearSafetyRanking";
import moment from "moment";

export default {
	components: { HandleTemplate },
	data() {
		return {
			abnormalId: null,
			abnormalData: {
				tableVal: {
					sourceCode: "",
					nuclideName: "",
					abnormalType: "",
					checkDose: "",
					abnormalTime: "",
					alarmContent: ""
				}
			},
			tableVal:null,
			menus: [],
			loading: false,
			chartData: {
				data: [],
				xdata: [],
				efData: [],
				vmap: []
			},
			listQuery: {
				id: '',
				startTime: '',
				endTime: ''
			}
		};
	},
	created() {
		this.menus = this.getMenu();
		this.abnormalId = this.$route.query.id;
		console.log(this.abnormalId);
	},
	mounted() {},
	methods: {
		// 获取左侧菜单
		getMenu() {
			let tableData = JSON.parse(sessionStorage.getItem("abnormalList"));
			// 列表名替换
			tableData.map(v => {
				this.$set(v, "name", v.enterprise + "（" + (v.status ?? ' - ') + "）");
				return v;
			});
			return tableData;
		},
		// 点击左侧菜单获取数据
		handleClick(value) {
			if (!value.id) return;
			this.loading = true;
			// 调接口获取
			getAbnormal({ id: value.id })
				.then(res => {
					this.tableVal = res.data.enterpriseData;
					this.loading = false;
					this.listQuery = {
						id: res.data.useSourceId,
						startTime: moment(res?.data?.startTime || new Date()).format('YYYY-MM-DD HH:mm'),
						endTime: moment(res?.data?.endTime || new Date()).format('YYYY-MM-DD HH:mm')
					}
					this.abnormalData = {
						tableVal: this.tableVal,
						conclusionCon: res.data.conclusionCon,
						isHandle: res.data.isHandle != 0,
						enterpriseName: this.menus.find(v => v.id === value.id)
							.enterprise
					};
				})
				.catch(error => {
					this.loading = false;
				});
		},
		/**
		 * @desc 重置左侧菜单状态
		 * @param {Object} value 点击处理的值 
		 */
		setNewMenuData(value) {
			let tableData;
			try {
				tableData = JSON.parse(sessionStorage.getItem("abnormalList"));
			} catch (error) {
				tableData = [];
			}
			tableData.find(item => value.id === item.id).status = "已处理";
			sessionStorage.setItem("abnormalList", JSON.stringify(tableData));
		},
		/**
		 * @desc 处理提交
		 * @param {Object} value 点击处理的值 
		 */
		submit(value) {
			this.$confirm("确认要提交吗？")
				.then(_ => {
					insertAlarmHandle(value)
						.then(res => {
							if (res.code == 200) {
								// this.handleClick(value)
								this.setNewMenuData(value);
								this.menus = this.getMenu();
								this.$message({
									message: "处理完成",
									type: "success"
								});
							} else {
								this.$message({
									message: "处理失败",
									type: "error"
								});
							}
						})
						.catch(err => {
							this.$message({
								message: "处理失败",
								type: "error"
							});
						});
				})
				.catch(_ => {});
		}
	}
};
</script>

<style lang="scss" scoped></style>
